<script setup>
  import { Edit, Delete } from '@element-plus/icons-vue'
  import { ref } from 'vue'
  import { getArticleChannelService, delArticleChannelService } from '@/api/article.js'
  import ChannelEdit from './components/ChannelEdit.vue'

  const channelList = ref([])
  const loading = ref(false)
  const dialog = ref()

  const getChannelList = async () => {
    loading.value = true
    const res = await getArticleChannelService()
    channelList.value = res.data.data
    // console.log(channelList.value)
    loading.value = false
  }
  getChannelList()

  // 删除
  const handleDeleteChannel = async (row) => {
    await ElMessageBox.confirm('你确定要删除该分类吗?', '温馨提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await delArticleChannelService(row.id)
    ElMessage.success('删除成功')
    getChannelList()
  }
  // 编辑
  const handleEditChannel = (row) => {
    dialog.value.open(row)
  }
  // 添加
  const handleAddChannel = () => {
    dialog.value.open({})
  }
  // 刷新列表
  const onSuccess = () => {
    getChannelList()
  }
</script>

<template>
  <PageContainer title="文章分类">
    <!-- 具名插槽 -->
    <template #extra>
      <el-button type="primary" @click="handleAddChannel">添加分类</el-button>
    </template>
    <!-- 默认插槽 -->
    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"></el-table-column>
      <el-table-column prop="cate_name" label="分类名称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作" width="150">
        <!-- 作用域插槽 -->
        <!-- row是channelList的一项,$index下标 -->
        <template #default="scope">
          <el-button
            :icon="Edit"
            circle
            type="primary"
            plain
            @click="handleEditChannel(scope.row, scope.$index)"
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            type="danger"
            plain
            @click="handleDeleteChannel(scope.row, scope.$index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="暂无数据"></el-empty>
      </template>
    </el-table>
    <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
  </PageContainer>
</template>

<style lang="scss" scoped></style>
